package com.lx.backstagemanagement.web;

import com.lx.backstagemanagement.common.ResultCode;
import com.lx.backstagemanagement.entity.User;
import com.lx.backstagemanagement.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "/users")
public class UserController {

    @Autowired
    private IUserService userServiceImpl;

    @GetMapping(value = "/{limit}/{pageSize}")
    public ResultCode getUsers(@PathVariable("limit") Integer limit, @PathVariable("pageSize") Integer pageSize) {
        if (StringUtils.isEmpty(limit)) {
            limit = 0;
        }
        Page<User> users = userServiceImpl.users(limit, pageSize);
        //users.getContent().stream().filter(entry -> "1".equals(entry.getUser_category())).collect(Collectors.toList());

        return ResultCode.getSuccessReturn(users);
    }

    @GetMapping(value = "/{uid}")
    public ResultCode editUser(@PathVariable("uid") String uid) {
        if (StringUtils.isEmpty(uid)) {
            return null;
        }
        User userById = userServiceImpl.getUserById(uid);
        //users.getContent().stream().filter(entry -> "1".equals(entry.getUser_category())).collect(Collectors.toList());

        return ResultCode.getSuccessReturn(userById);
    }

    @DeleteMapping(value = "/{uid}")
    public ResultCode deleteUser(@PathVariable("uid") String uid) {

        userServiceImpl.deleteUser(uid);

        return ResultCode.SUCCESS;
    }

    @PostMapping
    public ResultCode updateUser(@RequestBody User user) {

        userServiceImpl.update(user);
        return ResultCode.SUCCESS;
    }
}
